@using Microsoft.AspNetCore.Builder
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Http.Extensions
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Html
@using Microsoft.Extensions.Options
@using Microsoft.Extensions.Primitives
@inject Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer T
@inject Microsoft.Extensions.Options.IOptions<IdentityOptions> IdentityOptions
@inject IOptions<RequestLocalizationOptions> LocOptions
@inject OrchardCore.IOrchardHelper Orchard
@inject OrchardCore.Modules.IClock Clock
@model OrchardCore.Setup.ViewModels.SetupViewModel

@{
    // The Setup Layout is a standard MVC layout do that it doesn't use Shapes.
    Layout = "../_Layout.cshtml";
    var localTimeZone = Clock.GetSystemTimeZone();
    var defaultRecipe = Model.Recipes.FirstOrDefault(x => x.Name == Model.RecipeName) ?? Model.Recipes.FirstOrDefault();
    var options = IdentityOptions.Value;
    var passwordOptions = new HtmlContentBuilder();
    IHtmlContent separator = HtmlString.Empty;
    string passwordRegex = "";

    if (options.Password.RequireNonAlphanumeric)
    {
        passwordOptions.AppendHtml(separator).AppendHtml(T["one non-alphanumeric"]);
        separator = new HtmlString(", ");
        passwordRegex += @"(?=.*[^A-Za-z0-9])";
    }

    if (options.Password.RequireUppercase)
    {
        passwordOptions.AppendHtml(separator).AppendHtml(T["one uppercase"]);
        separator = new HtmlString(", ");
        passwordRegex += @"(?=.*[A-Z])";
    }

    if (options.Password.RequireLowercase)
    {
        passwordOptions.AppendHtml(separator).AppendHtml(T["one lowercase"]);
        separator = T[", "];
        passwordRegex += @"(?=.*[a-z])";
    }

    if (options.Password.RequireDigit)
    {
        passwordOptions.AppendHtml(separator).AppendHtml(T["one digit"]);
        separator = new HtmlString(", ");
        passwordRegex += @"(?=.*[0-9])";
    }

    if (separator != HtmlString.Empty)
    {
        separator = T[" and "];
    }

    passwordOptions.AppendHtml(separator).AppendHtml(T["{0} characters in total", options.Password.RequiredLength]);
    passwordRegex += $".{{{options.Password.RequiredLength},}}";

    if (options.Password.RequiredUniqueChars > 1)
    {
        passwordOptions.AppendHtml(T[", with {0} unique characters", options.Password.RequiredUniqueChars]);
    }

    var passwordTooltip = T["Password must have at least {0}.", passwordOptions];
}
<div class="form-group">
    @if (LocOptions.Value.SupportedUICultures.Count() > 1)
    {
        var query = new Dictionary<string, StringValues>(Context.Request.Query);
        query.Remove("ui-culture");
        var queryString = QueryString.Create(query);

        <div class="float-right form-inline p-2">
            <label for="culturesList" class="mr-2">@T["Change language"]</label>
            <select id="culturesList" class="form-control" onchange="setLocalizationUrl()">
                @foreach (var culture in LocOptions.Value.SupportedUICultures.OrderBy(x => x.NativeName))
                {
                    var uriString = UriHelper.BuildRelative(Context.Request.PathBase, Context.Request.Path, queryString.Add("ui-culture", culture.Name));

                    <option value="@culture" data-url="@uriString" selected="@(culture.Name == Orchard.CultureName())">@(culture.NativeName ?? culture.Name)</option>
                }
            </select>
        </div>
    }
</div>
<form asp-action="Index">
    <div class="bg-light p-2">
        <h1>@T["Setup"]</h1>
        <p class="lead">@T["Please answer a few questions to configure your site."]</p>
    </div>
    @if (defaultRecipe == null)
    {
        <div class="alert alert-danger" role="alert">
            <strong>@T["No recipes available."]</strong> @T["It is not possible to set up a site without a recipe."]
        </div>
    }

    <div class="row mt-2">
        <div class="form-group col-md-4" asp-validation-class-for="SiteName">
            <div>
                <label asp-for="SiteName">@T["What is the name of your site?"]</label>
                <input asp-for="SiteName" class="form-control" autofocus required />
                <span asp-validation-for="SiteName" class="text-danger">@T["The site name is required."]</span>
                <span class="text-muted form-text small">@T["This is used as the default title of your pages."]</span>
            </div>
        </div>
        @if (!Model.RecipeNamePreset)
        {
            <div class="form-group col-md-4">
                <input type="hidden" asp-for="RecipeName" required />
                <nav>
                    <div asp-validation-class-for="RecipeName">
                        <label asp-for="RecipeName">@T["Recipe"]</label>
                        @if (defaultRecipe == null)
                        {
                            <div id="noRecipes" class="dropdown disabled">
                                <button id="noRecipeButton" title="@T["No Recipes Available"]" class="btn btn-secondary dropdown-toggle disabled" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                    @T["No Recipes Available"]
                                </button>
                            </div>
                        }
                        else
                        {
                            <div id="recipes" class="dropdown">
                                <button id="recipeButton" title="@defaultRecipe.Description" class="btn btn-light dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                    @defaultRecipe.DisplayName
                                </button>
                                <div class="dropdown-menu">
                                    @foreach (var recipe in Model.Recipes.OrderBy(x => x.Name))
                                    {
                                        <a href="#" data-recipe-name="@recipe.Name" data-recipe-description="@recipe.Description" data-recipe-display-name="@recipe.DisplayName" class="dropdown-item">@recipe.DisplayName <span class="text-muted form-text small">@recipe.Description</span></a>
                                    }
                                </div>
                            </div>
                        }
                    </div>
                </nav>
                <span class="text-muted form-text small">@T["Recipes allow you to setup your site with additional pre-configured options, features and settings out of the box."]</span>
            </div>
        }
        <div class="form-group col-md-4" asp-validation-class-for="SiteTimeZone">
            <label asp-for="SiteTimeZone">@T["Default Time Zone"]</label>
            <select asp-for="SiteTimeZone" class="form-control">
                @foreach (var timezone in Clock.GetTimeZones())
                {
                    <option value="@timezone.TimeZoneId" selected="@(timezone.TimeZoneId == localTimeZone.TimeZoneId)">
                        @timezone
                    </option>
                }
            </select>
            <span asp-validation-for="SiteTimeZone"></span>
            <span class="text-muted form-text small">@T["Determines the default time zone used when displaying and editing dates and times."]</span>
        </div>
    </div>
    @if (!Model.DatabaseConfigurationPreset)
    {
        <div class="row">
            <div class="form-group col-md-6">
                <label asp-for="DatabaseProvider">@T["What type of database to use?"]</label>
                <select asp-for="DatabaseProvider" class="form-control" required>
                    @foreach (var provider in Model.DatabaseProviders)
                    {
                        <option value="@provider.Value" data-connection-string="@provider.HasConnectionString" data-table-prefix="@provider.HasTablePrefix" data-connection-string-sample="@T["The connection string to your database instance. e.g., {0}", provider.SampleConnectionString]">@provider.Name</option>
                    }
                </select>
                <span asp-validation-for="DatabaseProvider" class="text-danger"></span>
                <span class="text-muted form-text small">@T["The database is used to store the site's configuration and its contents."]</span>
            </div>

            <div class="form-group col-md-6 tablePrefix" asp-validation-class-for="TablePrefix">
                <label asp-for="TablePrefix">@T["Table Prefix"]</label>
                <input asp-for="TablePrefix" class="form-control" />
                <span asp-validation-for="TablePrefix" class="text-danger"></span>
                <span class="text-muted form-text small">@T["You can specify a table prefix if you intend to reuse the same database for multiple sites."]</span>
            </div>
        </div>

        <div class="row connectionString" asp-validation-class-for="ConnectionString">
            <div class="form-group col-md-12">
                <label asp-for="ConnectionString">@T["Connection string"]</label>
                <div class="input-group mb-2 mb-sm-0">
                    <input asp-for="ConnectionString" class="form-control pwd" type="password" required />
                    <div class="input-group-append reveal" title="@T["Show/hide connection string"]">
                        <button type="button" class="btn btn-secondary" tabindex="-1" aria-hidden="true"><i class="fa fa-eye-slash"></i></button>
                    </div>
                </div>
                <span asp-validation-for="ConnectionString" class="text-danger"></span>
                <span id="connectionStringHint" class="text-muted form-text small"></span>
            </div>
        </div>
    }
    <fieldset>
        <legend>@T["Super User"]</legend>
        <span class="text-muted form-text small">@T["The super user has all the rights. It should be used only during Setup and for disaster recovery."]</span>
        <div class="row">
            <div class="form-group col-md-6" asp-validation-class-for="UserName">
                <label asp-for="UserName">@T["User name"]</label>
                <input asp-for="UserName" class="form-control" required />
                <span asp-validation-for="UserName" class="text-danger">@T["The user name is required."]</span>
            </div>
            <div class="form-group col-md-6" asp-validation-class-for="Email">
                <label for="Email">@T["Email"]</label>
                <input asp-for="Email" class="form-control" type="email" required />
                <span asp-validation-for="Email" class="text-danger">@T["The email is invalid."]</span>
            </div>
        </div>
        <div class="row">
            <div class="form-group col-md-6" asp-validation-class-for="Password">
                <label asp-for="Password">@T["Password"]</label>
                <input asp-for="Password" type="password" class="form-control" data-toggle="popover" data-placement="top" data-trigger="focus" data-title="@T["Password strength"]" data-content="@passwordTooltip" required pattern="@passwordRegex" />
                <div id="passwordStrength"></div>
                <span asp-validation-for="Password" class="text-danger"></span>
            </div>

            <div class="form-group col-md-6" asp-validation-class-for="PasswordConfirmation">
                <label asp-for="PasswordConfirmation">@T["Password Confirmation"]</label>
                <input asp-for="PasswordConfirmation" type="password" class="form-control" title="@passwordTooltip" required pattern="@passwordRegex" />
                <span asp-validation-for="PasswordConfirmation" class="text-danger"></span>
            </div>
        </div>
    </fieldset>
    <div class="row">
        <div class="form-group col-md-4">
            <input type="hidden" asp-for="Secret" value="@Model.Secret" />
            <button class="btn btn-primary" type="submit" id="SubmitButton">@T["Finish Setup"]</button>
        </div>
    </div>
</form>
<script src="~/OrchardCore.Setup/Scripts/setup.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function(){
        $('#Password').strength({
            minLength: @(options.Password.RequiredLength),
            upperCase: @(options.Password.RequireUppercase ? "true" : "false"),
            lowerCase: @(options.Password.RequireLowercase ? "true" : "false"),
            numbers: @(options.Password.RequireDigit ? "true" : "false"),
            specialchars: @(options.Password.RequireNonAlphanumeric ? "true" : "false"),
            target: '#passwordStrength',
            style: "margin-top: 7px; height: 7px; border-radius: 5px"
        });

        $('#Password').popover({
            trigger: 'focus'
        });

        $(".reveal").on('click',function() {
            var $pwd = $(".pwd");
            if ($pwd.attr('type') === 'password') {
                $pwd.attr('type', 'text');
                $(this).find('button > i').toggleClass('fa-eye-slash fa-eye');
                $pwd.focus();
            } else {
                $pwd.attr('type', 'password');
                $(this).find('button > i').toggleClass('fa-eye fa-eye-slash');
                $pwd.focus();
            }
        });
    })
</script>